from commands.core.command import Command

class MoveCmd(Command):

    def __init__(self):
        self.names = ["move", "m"]
        self.has_args = True
   
    def convert_args(self, args: list):
        index = args[0]
        symb = index[0]
    
        if not symb in ["+", "-"]: symb = None

        try: return int(index), symb
        except: return 0, None


    def execute(self, args: list, obj):
        if len(args) == 0: return "Error: Incorrect arguments!"
        
        index, symb = self.convert_args(args)
        if index == 0: return "Error: Incorrect arguments!"

        if symb is None: 
            obj.index = index-1
            ln = len(obj.current_items)-1
            if obj.index > ln:
                obj.index = ln
            return 

        front = True
        if index < 0: 
            index *= -1
            front = False

        while index > 0:
            if front: obj.move_down()
            else: obj.move_up()
            index -= 1




